<?xml version="1.0" encoding="UTF-8"?>
<!--
/// 
/// MaDAson - Toolkit for Model Driven Software Development
///
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
-->
<%@ Page Language="C#" %>
<%@ Import Namespace="Madason.MetaModel" %>
<%@ Import Namespace="Madason.MetaModel.DAO" %>
<%@ Import Namespace="Madason.GenUtils" %>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="<%= Class.Name %>">

	<resultMap id="<%= Class.Name %>Result" class="<%= Class.ExplicitName %>">
<% 
    foreach (PersistedAttribute attr in Class.Attributes) 
    { 
        String nullValue = "";
        if (attr.Type.Name == "int") nullValue = "0";
        String nullDef = "nullValue=\""+nullValue+"\" ";
        
        if (attr.Type.Name == "Date") nullDef = "";
               
                
%>
		<result property="<%= attr.Name %>" column="<%= attr.ColumnName %>" javaType="<%= EnsureExplJavaType(attr.Type) %>" <%= nullDef %>/>
<% 
    } 
%>        
<% 
    foreach (PersistedAssociation assoc in Class.ActiveAssociations) 
    { 
        String nullValue = "";
        if (((Entity)assoc.TargetClass).PrimaryKey.Type.Name == "int") nullValue = "0";
        String nullDef = "nullValue=\""+nullValue+"\" ";
%>
		<result property="<%= assoc.Name %>" column="<%= assoc.ColumnName %>" javaType="<%= EnsureExplJavaType(((Entity)assoc.TargetClass).PrimaryKey.Type) %>" <%= nullDef %>/>
<% 
    } 
%>        
	</resultMap>


	<parameterMap id="<%= Class.Name %>Param" class="<%= Class.ExplicitName %>">
<% 
    foreach (PersistedAttribute attr in Class.Attributes) 
    { 
%>
		<parameter property="<%= attr.Name %>" javaType="<%= EnsureExplJavaType(attr.Type) %>"/>
<% 
    } 
%>        
<% 
    foreach (PersistedAssociation assoc in Class.ActiveAssociations) 
    {
		string nullStatement = "";
		if (((Entity)assoc.TargetClass).PrimaryKey.Type.Name == "int")
			nullStatement = " nullValue=\"0\" ";
			
%>
		<parameter property="<%= assoc.Name %>" <%= nullStatement %> javaType="<%= EnsureExplJavaType(((Entity)assoc.TargetClass).PrimaryKey.Type) %>" />
<% 
    } 
%>        
	</parameterMap>


	<insert id="insert" parameterClass="<%= Class.ExplicitName %>">
<% if (Context.DatabaseStrategy.ShouldUseSequenceForInsert(Class)) { %>
        <selectKey resultClass="int" keyProperty="<%= PrimaryKey.Name %>">
            SELECT <%= Class.SequenceName%>.nextval as <%= PrimaryKey.Name %> FROM dual
        </selectKey>
<% } %>
		
		<%= Class.GetInsertStatement() %>
	</insert>
	
	<update id="update" parameterClass="<%= Class.ExplicitName %>">
		UPDATE <%= Class.TableName%> 
		SET <%= Class.GetUpdateSetter()%>
		WHERE <%= PrimaryKey.ColumnName%> = <%= Context.DatabaseStrategy.InsertParameter(PrimaryKey.Name)%>
	</update>
	
	
	
	<%
		System.Collections.Hashtable finderHash = new System.Collections.Hashtable();
		foreach (Finder finder in Class.GetAndExpandFinder())
		{
			if (finderHash.Contains(finder.Name)) continue;
			finderHash.Add(finder.Name, finder);
	%>
	<select id="<%= finder.Name %>" resultMap="<%= Class.Name %>Result">
		
		<%= finder.GenerateSql().Replace("<", "&lt;").Replace(">", "&gt;") %>
	</select>	
	<% 
		}
	%>
		
	<%= protectedSection.GenForXml("USER_AREA") %>
</sqlMap>

<script runat="server">
	private Entity @class;

	
	public Entity Class
	{
		get { return @class; }
		set { @class = value; }
	}

	public PersistedAttribute PrimaryKey
	{
		get
		{
			return Class.PrimaryKey as PersistedAttribute;
		}
	}	
	
	protected string OperationReturnType(Operation finder)
	{
		return (finder.ReturnType.ExplicitName == "List") ?
		   "List<" + Class.Name + ">" :
		   finder.ReturnType.Name;
	}
	
	protected string FinderParamSep(Finder finder)	
	{
		return finder.Parameters.Count > 0 ? ", " : "";
	}

	private ProtectedSections protectedSection;

	public ProtectedSections ProtectedSection
	{
		get { return protectedSection; }
		set { protectedSection = value; }
	}

	protected String EnsureExplJavaType(Class cls)
	{
		if (cls.Name == "String") return "java.lang.String";
		else if (cls.Name == "Date") return "java.util.Date";
		else return cls.ExplicitName;
	}
	
</script>
